package com.example.administrator.xinyutg.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by Administrator on 2015/9/26.
 *
 *
 */
public class FirstUtil {
    /**
     * 判断是否为第一次运行程序
     * @return  true 表示为第一次运行
     */
    public static boolean isFirst(Context context){
        int versionCode=1;
        SharedPreferences sp=context.getSharedPreferences("spFirst",context.MODE_PRIVATE);
        versionCode=sp.getInt("version_key",1);
        boolean isFirst=true;
        if (isFirst || getVersion(context)!=versionCode ){
            sp.edit().putBoolean("appFirst",false);
            sp.edit().putInt("version_key",getVersion(context));
            return true;
        }

        return false;
    }

    /**
     * 得到程序版本号
     */
    public static int getVersion(Context context){
        int versionCode=1;
        PackageManager manager=context.getPackageManager();
        try {
            //得到版本号
            PackageInfo info=manager.getPackageInfo(context.getPackageName(),0);
            versionCode=info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return versionCode;
    }
}
